package me.zhengjie.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.kwService.PhoneService;
import me.zhengjie.kwService.PhoneStaffService;
import me.zhengjie.kwService.PhoneStudentService;
import me.zhengjie.kwService.PhoneTearcherService;
import me.zhengjie.pojo.dto.*;
import me.zhengjie.pojo.entity.Exam;
import me.zhengjie.util.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Long
 * @date 2021/7/6 10:06
 */
@RestController
@RequestMapping("/phone")
@Api(description = "PhoneController", tags = "手机端前端控制器")
@Slf4j
public class PhoneController {

    @Autowired
    private PhoneService phoneService;
    @Autowired
    private PhoneStudentService phoneStudentService;
    @Autowired
    private PhoneTearcherService phoneTearcherService;
    @Autowired
    private PhoneStaffService phoneStaffService;


    @ApiOperation("手机端登录")
    @PostMapping("/login")
    public BaseResponse phoneLogin(@RequestBody PhoneLoginDto dto) {
        try {
            return phoneService.phoneLogin(dto);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("手机端登录接口：" + e.getMessage());
            return BaseResponse.failed("登陆失败：" + e.getMessage());
        }
    }

    @ApiOperation("查询最新的一条考试公开信息")
    @GetMapping("/new-publicExam")
    public BaseResponse getNewPublicExam() {
        try {
            return BaseResponse.ok(phoneService.getNewPublicExam());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询最新的一条考试公开信息接口：" + e.getMessage());
            return BaseResponse.failed("查询失败：" + e.getMessage());
        }
    }


    @ApiOperation(value = "学生考试查询",notes = "如考试id为空，查询全部考试信息,如果传入了考试id，那么查询单个考试详情信息")
    @PostMapping("/find-student-exam")
    public BaseResponse findStudentExam(@RequestBody FindStudentExamDto dto, HttpServletRequest request) {
        try {
            return phoneStudentService.findStudentExam(dto, request);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("手机端学生考试查询接口：" + e.getMessage());
            return BaseResponse.failed("查询失败：" + e.getMessage());
        }
    }

    @ApiImplicitParam(name = "id", value = "考场考生安排表id", required = true)
    @ApiOperation("学生考试成绩详情查询")
    @PostMapping("/find-student-score")
    public BaseResponse findStudentScore(@RequestBody Integer id) {
        try {
            return BaseResponse.ok(phoneStudentService.findStudentScore(id));
        } catch (Exception e) {
            e.printStackTrace();
            log.info("手机端学生考试详情查询接口：" + e.getMessage());
            return BaseResponse.failed("查询失败：" + e.getMessage());
        }
    }

    @ApiOperation("学生成绩复核或成绩证明申请")
    @PostMapping("/save-score-status")
    public BaseResponse saveScoreStatus(@RequestBody @Validated PhoneScoreDto dto) {
        try {
            phoneStudentService.saveScoreStatus(dto);
            return BaseResponse.ok("申请成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("学生成绩复核或成绩证明申请接口：" + e.getMessage());
            return BaseResponse.failed("申请失败：" + e.getMessage());
        }
    }

    /*------------------------------------监考老师接口------------------------------------------------*/

    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码", required = true),
            @ApiImplicitParam(name = "size", value = "当前页码条数", required = true)
    })
    @ApiOperation("监考老师考试公开信息分页查询")
    @GetMapping("/find-public-exam")
    public BaseResponse findPublicExam(@RequestParam("current") Integer current, @RequestParam("size") Integer size) {
        try {
            return BaseResponse.ok(phoneTearcherService.findPublicExam(current, size));
        } catch (Exception e) {
            e.printStackTrace();
            log.info("监考老师考试公开信息分页查询接口：" + e.getMessage());
            return BaseResponse.failed("查询失败：" + e.getMessage());
        }
    }

    @ApiImplicitParam(name = "id", value = "考试公开信息id", required = true)
    @ApiOperation("监考老师考试公开信息查询单个")
    @GetMapping("/findOne-public-exam")
    public BaseResponse findOnePublicExam(@RequestParam("id") Integer id) {
        try {
            return BaseResponse.ok(phoneTearcherService.findOnePublicExam(id));
        } catch (Exception e) {
            e.printStackTrace();
            log.info("监考老师考试公开信息查询单个接口：" + e.getMessage());
            return BaseResponse.failed("查询失败：" + e.getMessage());
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码", required = true),
            @ApiImplicitParam(name = "size", value = "当前页码条数", required = true)
    })
    @ApiOperation("监考老师分页查询自己的监考服务")
    @GetMapping("/find-exam")
    public BaseResponse findExam(
            @RequestParam("current") Integer current,
            @RequestParam("size") Integer size,
            HttpServletRequest request) {
        try {
            return BaseResponse.ok(phoneTearcherService.findExam(current, size, request));
        } catch (Exception e) {
            e.printStackTrace();
            log.info("监考老师分页查询自己的监考服务接口：" + e.getMessage());
            return BaseResponse.failed("查询失败：" + e.getMessage());
        }
    }

    @ApiImplicitParam(name = "id", value = "考试id", required = true)
    @ApiOperation("监考老师查询监考详情")
    @GetMapping("/find-exam-detail")
    public BaseResponse findExamDetail(@RequestParam("id") Integer id, HttpServletRequest request) {
        try {
            return BaseResponse.ok(phoneTearcherService.findExamDetail(id, request));
        } catch (Exception e) {
            e.printStackTrace();
            log.info("监考老师查询考试详情接口：" + e.getMessage());
            return BaseResponse.failed("查询失败：" + e.getMessage());
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "考试id", required = true),
            @ApiImplicitParam(name = "current", value = "当前页码", required = true),
            @ApiImplicitParam(name = "size", value = "当前页码条数", required = true),
            @ApiImplicitParam(name = "examRoomId", value = "考场id", required = true)
    })
    @ApiOperation("监考老师分页查询本场考试考生详情")
    @GetMapping("/find-exam-student")
    public BaseResponse findExamStudent(
            @RequestParam("id") Integer id,
            @RequestParam("current") Integer current,
            @RequestParam("size") Integer size,
            @RequestParam("examRoomId") Integer examRoomId
    ) {
        try {
            return BaseResponse.ok(phoneTearcherService.findExamStudent(id, current, size, examRoomId));
        } catch (Exception e) {
            e.printStackTrace();
            log.info("监考老师分页查询本场考试考生详情接口：" + e.getMessage());
            return BaseResponse.failed("查询失败：" + e.getMessage());
        }
    }

    @ApiImplicitParam(name = "id", value = "考试id", required = true)
    @ApiOperation("监考老师签到")
    @PatchMapping("/checkin")
    public BaseResponse checkin(@RequestBody Integer id, HttpServletRequest request) {
        try {
            return BaseResponse.ok("签到成功", phoneTearcherService.checkin(id, request));
        } catch (Exception e) {
            e.printStackTrace();
            log.info("监考老师签到接口：" + e.getMessage());
            return BaseResponse.failed("签到失败：" + e.getMessage());
        }
    }

    @ApiImplicitParam(name = "id", value = "考试id", required = true)
    @ApiOperation("监考老师查询自己的签到信息")
    @PostMapping("/checkin-info")
    public BaseResponse checkinInfo(@RequestBody Integer id, HttpServletRequest request) {
        try {
            return BaseResponse.ok(phoneTearcherService.checkinInfo(id, request));
        } catch (Exception e) {
            e.printStackTrace();
            log.info("监考老师查询自己的签到信息接口：" + e.getMessage());
            return BaseResponse.failed("查询失败：" + e.getMessage());
        }
    }

    @ApiOperation("监考老师查询自己未监考的考试")
    @PostMapping("/find-exam")
    public BaseResponse findNotInExam(@RequestBody PageDto dto, HttpServletRequest request) {
        try {
            return BaseResponse.ok(phoneTearcherService.findNotInExam(dto, request));
        } catch (Exception e) {
            e.printStackTrace();
            log.info("监考老师查询自己未监考的考试接口：" + e.getMessage());
            return BaseResponse.failed("查询失败：" + e.getMessage());
        }
    }

    @ApiOperation("监考老师报名考试")
    @PostMapping("/enroll")
    public BaseResponse enroll(@RequestBody @Validated Exam exam, HttpServletRequest request) {
        try {
            return phoneTearcherService.enroll(exam, request);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("监考老师报名考试接口：" + e.getMessage());
            return BaseResponse.failed("报名失败：" + e.getMessage());
        }
    }





    /*------------------------------------------考务人员---------------------------------------------------*/


    @ApiOperation("考务人员查询考试信息")
    @PostMapping("/staff-find-exam")
    public BaseResponse getExam(@RequestBody @Validated PageDto dto, HttpServletRequest request) {
        try {
            return BaseResponse.ok(phoneStaffService.getExam(dto, request));
        } catch (Exception e) {
            e.printStackTrace();
            log.info("考务人员查询考试信息接口：" + e.getMessage());
            return BaseResponse.failed("查询失败：" + e.getMessage());
        }
    }

    @ApiImplicitParam(name = "id", value = "考试id", required = true)
    @ApiOperation("考务人员查询该场考试的所有考场信息")
    @PostMapping("/staff-findAll-exam-room")
    public BaseResponse findAllExamRoom(@RequestBody Integer id, HttpServletRequest request) {
        try {
            return BaseResponse.ok(phoneStaffService.findAllExamRoom(id, request));
        } catch (Exception e) {
            e.printStackTrace();
            log.info("考务人员查询该场考试的所有考场信息接口：" + e.getMessage());
            return BaseResponse.failed("查询失败：" + e.getMessage());
        }
    }

    @ApiImplicitParam(name = "id", value = "考场id", required = true)
    @ApiOperation("考务人员查询该考场的信息")
    @PostMapping("/staff-findOne-exam-room")
    public BaseResponse findOneExamRoom(@RequestBody Integer id) {
        try {
            return BaseResponse.ok(phoneStaffService.findOneExamRoom(id));
        } catch (Exception e) {
            e.printStackTrace();
            log.info("考务人员查询该考场的信息接口：" + e.getMessage());
            return BaseResponse.failed("查询失败：" + e.getMessage());
        }
    }

    @ApiOperation("考务人员校验考场")
    @PostMapping("/staff-check")
    public BaseResponse check(@RequestBody @Validated StaffCheckDto dto, HttpServletRequest request) {
        try {
            return BaseResponse.ok(phoneStaffService.check(dto, request));
        } catch (Exception e) {
            e.printStackTrace();
            log.info("考务人员校验考场接口：" + e.getMessage());
            return BaseResponse.failed("校验失败：" + e.getMessage());
        }
    }

}
